﻿using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;

namespace AMS.Inventory.PDA.Helper
{
    public class FormSaver
    {
        protected FormBuilder m_Builder;
        protected FormRuler m_Ruler;
        protected ObjXmlReader m_XmlReader;

        public FormSaver(string settingXmlPath)
        {
            m_Builder = new FormBuilder(settingXmlPath);
            m_XmlReader = new ObjXmlReader(settingXmlPath);
            m_Ruler = new FormRuler();
            SettingXmlPath = settingXmlPath;
        }

        public string SettingXmlPath { get; set; }

        public void SetObjDataRow(DataRow dr, Form frm, ref ArrayList alErrMsg)
        {
            XmlNodeList nlPros = m_Builder.GetProNodeList();
            foreach (XmlNode nPro in nlPros)
            {
                SetObjProToDataRow(dr, nPro, frm, ref alErrMsg);
            }
            //dr[INVENTORY_STATE] = "Updated";
        }

        public void SetObjProToDataRow(DataRow dr, XmlNode proNode, Form frm, ref ArrayList alErrMsg)
        {
            string strName = m_XmlReader.GetNodeAttributeValue(proNode, m_Builder.NAME, true);
            string strMemo = m_XmlReader.GetNodeAttributeValue(proNode, m_Builder.MEMO, true);
            if (dr == null || !dr.Table.Columns.Contains(strName))
                return;
            if (dr.Table.Columns.Contains(strName))
            {
                string strCtlName = m_Builder.OBJ_PRO_HEAD_STRING + strName;
                Control c = m_Builder.FindControl(frm, strCtlName);
                if (c != null)
                {
                    PropertyInfo pi = c.GetType().GetProperty(m_Builder.TEXT);
                    if (pi != null)
                    {
                        object objVal = pi.GetValue(c, null);
                        Type colType = dr.Table.Columns[strName].DataType;
                        string strVal = objVal.ToString();
                        if (colType.Equals(typeof (DateTime)))
                        {
                            if (strVal.Length == 0)
                                objVal = new DateTime(1900, 1, 1).ToShortDateString();
                            else
                                objVal = Convert.ToDateTime(strVal);
                        }
                        else if (colType.Equals(typeof (int)) || colType.Equals(typeof (long)) ||
                                 colType.Equals(typeof (Single))
                                 || colType.Equals(typeof (float)) || colType.Equals(typeof (uint)) ||
                                 colType.Equals(typeof (ushort))
                                 || colType.Equals(typeof (double)) || colType.Equals(typeof (ulong)) ||
                                 colType.Equals(typeof (decimal))
                                 || colType.Equals(typeof (double)))
                        {
                            if (strVal.Length == 0)
                                objVal = 0;
                        }
                        bool isErr = false;
                        XmlNodeList nl = m_XmlReader.GetChildNodesFromXML(proNode, FormRuler.FORMAT_RULE, true);
                        if (c.BackColor == Color.Red)
                        {
                            isErr = true;
                            alErrMsg.Add("【" + strMemo + "】" + "值不在范围内");
                        }
                        else
                        {
                            c.BackColor = Color.Transparent;
                            for (int i = 0; nl != null && i < nl.Count; i++)
                            {
                                XmlNode ruleNode = nl[i];
                                int length = 0;
                                string strMsg = "";
                                string strRuleType = m_XmlReader.GetNodeAttributeValue(ruleNode, FormRuler.TYPE_NAME,
                                                                                       true);
                                try
                                {
                                    length =
                                        Convert.ToInt32(m_XmlReader.GetNodeAttributeValue(ruleNode, FormRuler.LENGTH,
                                                                                          true));
                                }
                                catch
                                {
                                    length = 0;
                                }
                                if (length > 0)
                                {
                                    if (objVal.ToString().Length > length)
                                    {
                                        strMsg = FormRuler.m_IengthErg;
                                    }
                                }
                                strMsg += m_Ruler.CheckProValByFormatRule(objVal.ToString(), strRuleType);
                                if (strMsg.Length > 0)
                                {
                                    if (alErrMsg == null)
                                        alErrMsg = new ArrayList();
                                    alErrMsg.Add("【" + strMemo + "】" + strMsg);
                                    c.BackColor = Color.Pink;
                                    isErr = true;
                                }
                            }
                        }
                        if (!isErr)
                            dr[strName] = objVal;
                    }
                }
            }
        }
    }
}